package com.info.dpunified.web.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.info.dpunified.defects.DefectsSelector;
import com.info.dpunified.defects.transfer.DefectsSelection;

public class DefectReportServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3226901196139409997L;
	
	public void doGet(HttpServletRequest request,  
            HttpServletResponse response) throws IOException,  
                                                 ServletException {  
		doPost(request, response);  
	}  

	public void doPost(HttpServletRequest request,HttpServletResponse response)
	throws ServletException, IOException
	{
		DefectsSelection defectsSelection = new DefectsSelection();
		defectsSelection.setYear(request.getParameter("selectYear"));
		defectsSelection.setMonth(request.getParameter("selectMonth"));
		defectsSelection.setProjectCode(request.getParameter("selectProject"));
		defectsSelection.setImpact(request.getParameter("selectImpact"));
		defectsSelection.setStatus(request.getParameter("selectStatus"));
		defectsSelection.setSeverity(request.getParameter("selectSeverity"));
		defectsSelection.setPriority(request.getParameter("selectPriority"));
		defectsSelection.setType(request.getParameter("selectType"));
		
		DefectsSelector defectsSelector = new DefectsSelector();
		
		List<DefectsSelection> defectsReport = defectsSelector.fetchDefectsReport(defectsSelection);
		
		if(defectsReport == null || defectsReport.isEmpty())
		{
			request.setAttribute("defects", "notfound");
			request.setAttribute("defectsnotfound", "No defects found for the selected criteria. Please refilter your search");
		}
		
		request.setAttribute("DEFECTSREPORT", defectsReport);
		
		try {
			getServletConfig().getServletContext().getRequestDispatcher("/jsp/defectreport.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
